package main

func main() {
	l1 := &ListNode{3, &ListNode{0, &ListNode{5, nil}}}
	l2 := &ListNode{3, &ListNode{0, &ListNode{3, nil}}}
	addTwoNumbers(l1, l2)
}

type ListNode struct {
	Val  int
	Next *ListNode
}

//两数之和匹配
func twoSum(nums []int, target int) []int {
	temp := make(map[int]int, len(nums))
	for k, v := range nums {
		n, ok := temp[v]
		if ok {
			return []int{n, k}
		}
		temp[target-v] = k
	}
	return []int{-1, -1}
}

//两数相加2
func addTwoNumbers(l1 *ListNode, l2 *ListNode) *ListNode {
	r := &ListNode{}
	rr := r
	for l1 != nil || l2 != nil {
		if l1 != nil {
			r.Val += l1.Val
			l1 = l1.Next
		}
		if l2 != nil {
			r.Val += l2.Val
			l2 = l2.Next
		}
		r.Next = &ListNode{r.Val / 10, nil}
		r.Val = r.Val % 10
		r = r.Next
	}
	return rr
}
